﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using HiLand.General.Entity;
using HiLand.General.BLL;
using HiLand.Utility.Enums;

namespace GBFinance.Web.Models
{
    public class LoanBasicSetting
    {
        private List<BasicSettingEntity> basicSettingList = null;
        private List<BasicSettingEntity> BasicSettingList
        {
            get
            {
                if (basicSettingList == null)
                {
                    basicSettingList = BasicSettingBLL.GetList(Logics.False, string.Empty);
                }

                return basicSettingList;
            }
        }

        public decimal SecuredLoansAmountMin
        {
            get
            {
                return GetSettingValue("SecuredLoansAmountMin", 5000);
            }
        }

        public decimal SecuredLoansAmountMax
        {
            get
            {
                return GetSettingValue("SecuredLoansAmountMax", 50000);
            }
        }

        public decimal UnSecuredLoansAmountMin
        {
            get
            {
                return GetSettingValue("UnSecuredLoansAmountMin", 1000);
            }
        }

        public decimal UnSecuredLoansAmountMax
        {
            get
            {
                return GetSettingValue("UnSecuredLoansAmountMax", 5000);
            }
        }

        public decimal SecuredLoansAnnualInterest
        {
            get
            {
                return GetSettingValue("SecuredLoansAnnualInterest", 0.48M);
            }
        }

        public decimal UnSecuredLoansAnnualInterest
        {
            get
            {
                return GetSettingValue("UnSecuredLoansAnnualInterest", 0.48M);
            }
        }

        private decimal GetSettingValue(string settingKey, decimal defaultValue)
        {
            decimal securedLoansAmountMin = defaultValue;
            BasicSettingEntity entity = BasicSettingList.Single(currentEntity => currentEntity.SettingKey == settingKey);
            if (entity != null)
            {
                decimal.TryParse(entity.SettingValue, out  securedLoansAmountMin);
            }

            return securedLoansAmountMin;
        }
    }
}